package dongshi.daddy.stream.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import dongshi.daddy.stream.beans.CouponTemplateInfo;
import dongshi.daddy.stream.beans.PagedCouponTemplateInfo;
import dongshi.daddy.stream.beans.TemplateSearchParams;
import dongshi.daddy.stream.service.intf.CouponTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collection;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/template")
public class CouponTemplateController {

    @Autowired
    private CouponTemplateService couponTemplateService;

    // 创建优惠券
    @PostMapping("/addTemplate")
    public CouponTemplateInfo addTemplate(@Valid @RequestBody CouponTemplateInfo request) {
        log.info("Create coupon template: data={}", request);
        return couponTemplateService.createTemplate(request);
    }

    @PostMapping("/cloneTemplate")
    public CouponTemplateInfo cloneTemplate(@RequestParam("id") Long templateId) {
        log.info("Clone coupon template: data={}", templateId);
        return couponTemplateService.cloneTemplate(templateId);
    }

    // 读取优惠券
    @GetMapping("/getTemplate")
    @SentinelResource(value = "getTemplate")
    public CouponTemplateInfo getTemplate(@RequestParam("id") Long id){
        log.info("Load template, id={}", id);
        /*try {
            // 休眠二十秒模拟超时
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return couponTemplateService.loadTemplateInfo(id);
    }

    // 批量获取
    @GetMapping("/getBatch22")
    // 降级要执行的方法
//    @SentinelResource(value = "getTemplateInBatch", blockHandler = "getTemplateInBatch_block")
//    public JSONObject getBatch22() {
    public JSONArray getBatch22() {

        /*log.info("getTemplateInBatch: {}", JSON.toJSONString(ids));
        return couponTemplateService.getTemplateInfoMap(ids);*/
        String xx = "{\"error\":0,\"status\":1000,\"data\":{\"expect\":\"slot\",\"intentionID\":1616,\"qid\":37130,\"qstr\":\"查询还款日\",\"answer\":\"您好，小恒给您发送了一条验证短信，请在您的预留手机上进行查看，并输入验证码以井号键结束，请勿透露给他人，小恒在此表示感谢！\",\"slot\":[{\"name\":\"idcard\",\"var_type\":\"@sys.idcard\",\"required\":1,\"default_value\":\"\",\"orig\":\"\",\"value\":\"120110198705036489\",\"prompt\":[{\"weight\":100,\"question\":\"为确保您的账户安全，请输入您的身份证号码，以井号键结束\"}]},{\"name\":\"mobile\",\"var_type\":\"@sys.mobile\",\"required\":1,\"default_value\":\"\",\"orig\":\"\",\"value\":\"18614093048\",\"prompt\":[{\"weight\":100,\"question\":\"若您的预留手机号为来电手机号则按1，否则请输入预留手机号并井号键结束\"}]},{\"name\":\"mobile_code\",\"var_type\":\"@sys.mobile_code\",\"required\":1,\"default_value\":\"\",\"orig\":\"\",\"value\":\"\",\"prompt\":[{\"weight\":100,\"question\":\"您好，小恒给您发送了一条验证短信，请在您的预留手机上进行查看，并输入验证码以井号键结束，请勿透露给他人，小恒在此表示感谢！\"}]}],\"debug\":null}}";
        JSONArray resultArr = new JSONArray();
        resultArr.add(JSONObject.parseObject(xx));
        resultArr.add(JSONObject.parseObject(xx));
        resultArr.add(JSONObject.parseObject(xx));
        resultArr.add(JSONObject.parseObject(xx));
        resultArr.add(JSONObject.parseObject(xx));
        resultArr.add(JSONObject.parseObject(xx));
        resultArr.add(JSONObject.parseObject(xx));
        resultArr.add(JSONObject.parseObject(xx));
        resultArr.add(JSONObject.parseObject(xx));
        resultArr.add(JSONObject.parseObject(xx));
        resultArr.add(JSONObject.parseObject(xx));
        return resultArr;
    }

    // 批量获取
    @GetMapping("/getBatch")
    // 降级要执行的方法
//    @SentinelResource(value = "getTemplateInBatch", blockHandler = "getTemplateInBatch_block")
    public Map<Long, CouponTemplateInfo> getTemplateInBatch(@RequestParam("ids") Collection<Long> ids) {
        log.info("getTemplateInBatch22222: {}", JSON.toJSONString(ids));
        log.warn("xxx数据是0条，可能是数据错误了。");
        int x = 1 / 0;
        return couponTemplateService.getTemplateInfoMap(ids);
    }

    // 批量获取
    @GetMapping("/getBatchV1")
    // 降级要执行的方法
    @SentinelResource(value = "getTemplateInBatchV1", fallback = "getTemplateInBatch_fallback")
    public Map<Long, CouponTemplateInfo> getTemplateInBatchV1(@RequestParam("ids") Collection<Long> ids) {
        log.info("getTemplateInBatch: {}", JSON.toJSONString(ids));
        int i = 1 / 0;
        return couponTemplateService.getTemplateInfoMap(ids);
    }

    public Map<Long, CouponTemplateInfo> getTemplateInBatch_fallback(Collection<Long> ids) {
        log.info("接口被fallback");
        return Maps.newHashMap();
    }

    public Map<Long, CouponTemplateInfo> getTemplateInBatch_block(
            Collection<Long> ids, BlockException exception) {
        log.info("接口被限流");
        return Maps.newHashMap();
    }
    // 搜索模板
    @PostMapping("/search")
    public PagedCouponTemplateInfo search(@Valid @RequestBody TemplateSearchParams request) {
        log.info("search templates, payload={}", request);
        return couponTemplateService.search(request);
    }

    // 优惠券无效化
    @DeleteMapping("/deleteTemplate")
    public void deleteTemplate(@RequestParam("id") Long id){
        log.info("Load template, id={}", id);
        couponTemplateService.deleteTemplate(id);
    }
}
